﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Helper;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Common.SetIfActivePage(this.Page, "Default.aspx", "active");

        ManageMenuItem.Visible = false;
        ReportMenuItem.Visible = false;
        UploadMenuItem.Visible = false;

        bool userIsLoggedIn = (System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated;

        if (userIsLoggedIn)
        {
            if (SessionHelper.UserInfo != null)
            {
                loggedInUsername.InnerHtml = string.Format("{0} {1}", SessionHelper.UserInfo.FirstName, SessionHelper.UserInfo.LastName);

                ConfigureUserMenuAccess(SessionHelper.UserInfo);
            }
            
            loggedInContainer.Visible = true;
            loggedOutContainer.Visible = false;
        }
        else
        {
            loggedInContainer.Visible = false;
            loggedOutContainer.Visible = true;
        }
    }

    public void ConfigureUserMenuAccess(User user)
    {
        if (user != null)
        {
            if (user.UserRole != null)
            {
                if (string.Compare(user.UserRole.Name, "Administrator") == 0)
                {
                    ManageMenuItem.Visible = true;
                }
                else
                {
                    ManageMenuItem.Visible = false;
                }

                if (string.Compare(user.UserRole.Name, "Administrator") == 0 || string.Compare(user.UserRole.Name, "Finance Manager")==0)
                {
                    UploadMenuItem.Visible = true;
                    ReportMenuItem.Visible = true;
                }
                else
                {
                    UploadMenuItem.Visible = false;
                    ReportMenuItem.Visible = false;
                }
            }
        }
    }
}
